/**
 * 
 */
package com.cplsystems.incidencias.vm;

import java.util.List;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Div;
import org.zkoss.zul.Include;

import com.cplsystems.incidencias.app.MetaData;
import com.cplsystems.incidencias.app.utils.IncidenciasConstants;
import com.cplsystems.incidencias.app.utils.IncidenciasUtils;
import com.cplsystems.incidencias.app.utils.MetaAplicationIntegrityChecker;

/**
 * @author César Palalía López (csr.plz@aisa-automation.com)
 * 
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class HomeVM extends MetaData {

	private static final long serialVersionUID = -6598753761973861983L;
	@Wire("#header")
	private Div header;
	@Wire("#menu")
	private Div menu;
	@Wire("#content")
	private Div content;
	@Wire("#footer")
	private Div footer;

	@Init
	public void init() {
		sessionUtils.getFromSession(IncidenciasUtils.decrypt(IncidenciasConstants.FIRMA));
		/*
		Organizacion organizacion = (Organizacion) sessionUtils
				.getFromSession(IncidenciasUtils
						.encrypt(IncidenciasConstants.FIRMA));
		if (organizacion == null) {
			incidenciasUtils
					.redirect(IncidenciasConstants.GLOBAL_PAGES.LOGIN_URL);
			return;
		}*/

		// DatosGenerales datosGenerales =
		// datosGeneralesService.getDatosGeneralesByPersona(persona);

		IncidenciasUtils.showMessage("Bienvenido",
				Clients.NOTIFICATION_TYPE_INFO, 3000);
		/*
		 * IncidenciasUtils.showMessage(persona.getSexo().equals(Sexo.MASCULINO)
		 * ? "Bienvenido Sr " : "Bienvenida Sra " +
		 * datosGenerales.getNombreCompleto(), Clients.NOTIFICATION_TYPE_INFO,
		 * 3000);
		 */
	}

	@AfterCompose
	public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
		Selectors.wireComponents(view, this, false);
	}

	@NotifyChange("*")
	@GlobalCommand
	public void updateWorkArea(
			@BindingParam(MenuVM.PAGE_TO_RENDER) String pageToRender) {
		List<Component> components = content.getChildren();
		if (components != null) {
			components.clear();
			content.appendChild(new Include(pageToRender));
		}
	}

}
